home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / misc / xref_v1.1.lha / XRef / Tools / source / parsexref.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-09  |  10.0 KB  |  378 lines

  1. ;/* execute me to make with SAS 6.x
  2. sc NOSTKCHK CSRC parsexref.c OPT IGNORE=73
  3. slink lib:c.o parsexref.o //Goodies/extrdargs/extrdargs.o TO /c/parsexref SMALLDATA SMALLCODE NOICONS LIB lib:amiga.lib lib:sc.lib /lib/xrefsupport.lib
  4. quit
  5. */
  6.  
  7. /*
  8. ** $PROJECT: XRef-Tools
  9. **
  10. ** $VER: parsexref.c 1.5 (06.07.94)
  11. **
  12. ** by
  13. **
  14. ** Stefan Ruppert , Windthorststraße 5 , 65439 Flörsheim , GERMANY
  15. **
  16. ** (C) Copyright 1994
  17. ** All Rights Reserved !
  18. **
  19. ** $HISTORY:
  20. **
  21. ** 06.07.94 : 001.005 :  changed to new parsemessage
  22. ** 28.05.94 : 001.004 :  now uses the tags function and template output added
  23. ** 18.05.94 : 001.003 :  ctrl-c supported
  24. ** 02.05.94 : 001.002 :  changed to xref.library
  25. ** 26.04.94 : 000.001 : initial
  26. **
  27. */
  28.  
  29. #include "Def.h"
  30.  
  31. #include <libraries/xref.h>
  32. #include <proto/xref.h>
  33.  
  34. #include <debug.h>
  35. #include <register.h>
  36.  
  37. #include "parsexref_rev.h"
  38.  
  39. /* ------------------------------ structures ------------------------------ */
  40.  
  41. enum {
  42.    OT_TYPE,
  43.    OT_PATH,
  44.    OT_FILE,
  45.    OT_NAME,
  46.    OT_XREFNAME,
  47.    OT_LINE,
  48.    OT_NODENAME,
  49.    OT_MAX,
  50.    };
  51.  
  52. struct OutputTemplate
  53. {
  54.    STRPTR ot_Template;
  55.    ULONG ot_Para[OT_MAX];
  56.    BYTE ot_Index[OT_MAX];
  57.    BYTE ot_Type;
  58.    BYTE ot_Path;
  59.    BYTE ot_File;
  60.    BYTE ot_Name;
  61.    BYTE ot_XRefName;
  62.    BYTE ot_Line;
  63.    UBYTE ot_Buffer[20];
  64. };
  65.  
  66. #define SETOTPARA(ot,index,value)    { if(ot->ot_Index[index] >= 0) \
  67.                                            ot->ot_Para[ot->ot_Index[index]] = (ULONG) value; \
  68.                                      }
  69.  
  70. /* ---------------------------- version string ---------------------------- */
  71.  
  72. static char *version = VERSTAG;
  73. static char *prgname = "ParseXRef";
  74.  
  75. /* ------------------------- template definition -------------------------- */
  76.  
  77. #define template "STRING/A,CATEGORY,FILE/K,LIMIT/K/N,FORMAT/K,NOPATTERN/S,NOCASE/S"
  78.  
  79. enum {
  80.    ARG_STRING,
  81.    ARG_CATEGORY,
  82.    ARG_FILE,
  83.    ARG_LIMIT,
  84.    ARG_FORMAT,
  85.    ARG_NOPATTERN,
  86.    ARG_NOCASE,
  87.    ARG_MAX};
  88.  
  89. static const STRPTR mytypes[] = {
  90.    "generic",
  91.    "function",
  92.    "command",
  93.    "include",
  94.    "macro",
  95.    "struct",
  96.    "field",
  97.    "typedef",
  98.    "define",
  99.    NULL};
  100.  
  101. /* ------------------------------- AutoDoc -------------------------------- */
  102.  
  103. /*FS*/ /*"AutoDoc"*/
  104. /*GB*** XRef-Tools/ParseXRef *************************************************
  105.  
  106. $VER: ParseXRef.doc
  107.  
  108. NAME
  109.     ParseXRef - parse the specified xreffiles from xref.library and prints it
  110.                 to stdout
  111.  
  112. TEMPLATE
  113.     STRING/A,CATEGORY,FILE/K,LIMIT/K/N,FORMAT/K,NOPATTERN/S,NOCASE/S
  114.  
  115. FORMAT
  116.     ParseXRef [STRING] string [FILE xreffile] [[CATEGORY] category]
  117.               [LIMIT number] [FORMAT format-string] [NOPATTERN] [NOCASE]
  118.  
  119. FUNCTION
  120.     parses the specified xreffiles (CATEGORY or FILE argument) for a given
  121.     string/pattern. If no file or category is given it parses all xreffile,
  122.     which are actually loaded from the xref.library.If a category is specified
  123.     only such xreffiles are parsed, which match the category.If a file is
  124.     given only the file will parsed.If you turn the NOPATTERN switch on, the
  125.     string you passed via STRING argument is interpreted as a normal string
  126.     without any patternmatching tokens.It is passed to a normal string compare
  127.     function strcmp() or Stricmp(), otherwise to MatchPattern() or 
  128.     MatchPatternNoCase().Normally it is case-sensetive, but if you turn the
  129.     NOCASE switch on it isn't.
  130.  
  131. INPUTS
  132.     STRING (STRING) - string/pattern to parse for
  133.  
  134.     FILE (STRING) - file for the parse
  135.  
  136.     CATEGORY (STRING) - category for the parse (parse all files with this
  137.         category) . Can be a pattern !
  138.  
  139.     LIMIT (LONG) - maximal number of matching entries
  140.  
  141.     FORMAT (STRING) - format-string for the output to stdout following format
  142.         characters are supported after a '%' character :
  143.         %T - name of the XRefEntry type (e.g."AmigaGuide Node" or "struct" ..)
  144.         %P - global path for the entry
  145.         %F - file with a relative path for the entry
  146.         %N - name of the file
  147.         %X - XRefEntry name
  148.         %L - line number in the file for the XRefEntry
  149.         %G - AmigaGuide nodename
  150.         default template : "%T %X found !\nIn File : %F , Line : %L\n"
  151.  
  152.     NOPATTERN (BOOL) - indicates, that the entries are compares with strcmp
  153.         functions , instead of using MatchPattern()
  154.  
  155.     NOCASE (BOOL) - indicates, that case-insensetive compare should used
  156.  
  157. SEE ALSO
  158.     AGuideXRefV37, AGuideXRefV39, MakeXRef, LoadXRef, ExpungeXRef,
  159.     xref.library/ParseXRef(), dos.library/MatchPattern()
  160.  
  161. COPYRIGHT
  162.     (C) by Stefan Ruppert 1994
  163.  
  164. HISTORY
  165.     ParseXRef 1.6 (3.9.94) :
  166.         - changed some tag names
  167.  
  168.     ParseXRef 1.5 (6.7.94) :
  169.         - changed to new parsehook
  170.  
  171.     ParseXRef 1.4 (28.5.94) :
  172.         - output-template added
  173.  
  174.     ParseXRef 1.3 (18.5.94) :
  175.         - control-c support added
  176.         - first beta release
  177.  
  178. *****************************************************************************/
  179. /*FE*/
  180.  
  181. /* --------------------------- parse hook entry --------------------------- */
  182.  
  183. RegCall GetA4 ULONG parsehook(REGA0 struct Hook *hook,REGA2 struct XRefFileNode *xref,REGA1 struct xrmXRef *msg)
  184. {
  185.    struct OutputTemplate *optmp = (struct OutputTemplate *) hook->h_Data;
  186.  
  187.    if(msg->Msg == XRM_XREF)
  188.    {
  189.       struct TagItem *tstate = msg->xref_Attrs;
  190.       struct TagItem *tag;
  191.       ULONG tidata;
  192.       UBYTE type;
  193.  
  194.       while((tag = NextTagItem(&tstate)))
  195.       {
  196.          tidata = tag->ti_Data;
  197.  
  198.          switch(tag->ti_Tag)
  199.          {
  200.          case ENTRYA_Type:
  201.             type = tidata;
  202.             if(type >= XREFT_MAXTYPES)
  203.                break;
  204.       
  205.             SETOTPARA(optmp,OT_TYPE,mytypes[type]);
  206.             break;
  207.          case ENTRYA_File:
  208.             if(tidata)
  209.             {
  210.                SETOTPARA(optmp,OT_FILE,tidata);
  211.                SETOTPARA(optmp,OT_NAME,FilePart((STRPTR) tidata));
  212.             }
  213.             break;
  214.          case ENTRYA_Name:
  215.             SETOTPARA(optmp,OT_XREFNAME,tidata);
  216.             break;
  217.          case ENTRYA_Line:
  218.             sprintf(optmp->ot_Buffer,"%ld",tidata);
  219.             SETOTPARA(optmp,OT_LINE,optmp->ot_Buffer);
  220.             break;
  221.          case ENTRYA_NodeName:
  222.             SETOTPARA(optmp,OT_NODENAME,tidata);
  223.             break;
  224.          case XREFA_Path:
  225.             SETOTPARA(optmp,OT_PATH,tidata);
  226.             break;
  227.          }
  228.       }
  229.  
  230.       if(type < XREFT_MAXTYPES)
  231.          VPrintf(optmp->ot_Template,optmp->ot_Para);
  232.       else
  233.          Printf ("Unknown XRef Type : %ld\n",type);
  234.  
  235.    } else
  236.       Printf ("Not supported hook message : %ld\n",msg->Msg);
  237.  
  238.    if(SetSignal(0L,SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
  239.    {
  240.       SetIoErr(ERROR_BREAK);
  241.       return(1);
  242.    }
  243.  
  244.    return(0);
  245. }
  246.  
  247. /* ---------------------------- main function ----------------------------- */
  248.  
  249. int main(int ac,char *av[])
  250. {
  251.    struct ExtRDArgs eargs = {NULL};
  252.    struct Library *XRefBase;
  253.  
  254.    ULONG para[ARG_MAX];
  255.    STRPTR obj = prgname;
  256.    LONG err;
  257.  
  258.    LONG i;
  259.  
  260.    /* clear args buffer */
  261.    for(i = 0 ; i < ARG_MAX ; i++)
  262.       para[i] = 0;
  263.  
  264.    eargs.erda_Template      = template;
  265.    eargs.erda_Parameter     = para;
  266.    eargs.erda_FileParameter = ARG_FILE;
  267.    eargs.erda_Window        = "CON:////ParseXRef/CLOSE/WAIT";
  268.  
  269.    if((err = ExtReadArgs(ac,av,&eargs)) == 0)
  270.    {
  271.       obj = "xref.library";
  272.       if(XRefBase = OpenLibrary(obj,0))
  273.       {
  274.          struct Hook hook = {NULL};
  275.          struct OutputTemplate optmp;
  276.          ULONG matching = XREFMATCH_PATTERN_CASE;
  277.          STRPTR output = (para[ARG_FORMAT]) ? (STRPTR) para[ARG_FORMAT] :
  278.                                               (STRPTR) "%T %X found !\nIn File : %F , Line : %L\n";
  279.          STRPTR ptr;
  280.          BYTE cnt = 0;
  281.  
  282.          obj = prgname;
  283.  
  284.          if(para[ARG_NOPATTERN])
  285.             if(para[ARG_NOCASE])
  286.                matching = XREFMATCH_COMPARE_NOCASE;
  287.             else
  288.                matching = XREFMATCH_COMPARE_CASE;
  289.          else
  290.             if(para[ARG_NOCASE])
  291.                matching = XREFMATCH_PATTERN_NOCASE;
  292.  
  293.          hook.h_Entry = (HOOKFUNC) parsehook;
  294.          hook.h_Data  = &optmp;
  295.  
  296.          for(i = 0 ; i < OT_MAX ; i++)
  297.          {
  298.             optmp.ot_Index[i] = -1;
  299.             optmp.ot_Para[i]  = NULL;
  300.          }
  301.  
  302.          for(ptr = output; *ptr ; ptr++)
  303.          {
  304.             if(*ptr == '%')
  305.             {
  306.                UBYTE chr = *(++ptr);
  307.  
  308.                *ptr = 's';
  309.  
  310.                switch(chr)
  311.                {
  312.                case 't':
  313.                case 'T':
  314.                   optmp.ot_Index[OT_TYPE] = cnt;
  315.                   break;
  316.                case 'p':
  317.                case 'P':
  318.                   optmp.ot_Index[OT_PATH] = cnt;
  319.                   break;
  320.                case 'f':
  321.                case 'F':
  322.                   optmp.ot_Index[OT_FILE] = cnt;
  323.                   break;
  324.                case 'n':
  325.                case 'N':
  326.                   optmp.ot_Index[OT_NAME] = cnt;
  327.                   break;
  328.                case 'x':
  329.                case 'X':
  330.                   optmp.ot_Index[OT_XREFNAME] = cnt;
  331.                   break;
  332.                case 'l':
  333.                case 'L':
  334.                   optmp.ot_Index[OT_LINE] = cnt;
  335.                   break;
  336.                case 'g':
  337.                case 'G':
  338.                   optmp.ot_Index[OT_NODENAME] = cnt;
  339.                   break;
  340.                case '\0':
  341.                   ptr--;
  342.                   break;
  343.                default:
  344.                   *ptr = '%';
  345.                   cnt--;
  346.                }
  347.                cnt++;
  348.                ptr++;
  349.             }
  350.          }
  351.  
  352.          optmp.ot_Template = output;
  353.  
  354.          ParseXRefTags((STRPTR) para[ARG_STRING],XREFA_Category   ,para[ARG_CATEGORY],
  355.                                                  XREFA_Matching   ,matching,
  356.                                                  XREFA_XRefHook   ,&hook,
  357.                                                  XREFA_Limit      ,(para[ARG_LIMIT]) ? (*((LONG *) para[ARG_LIMIT])) : (ULONG) ~0,
  358.                                                  XREFA_File       ,para[ARG_FILE],
  359.                                                  TAG_DONE);
  360.  
  361.          CloseLibrary(XRefBase);
  362.       }
  363.    }
  364.    ExtFreeArgs(&eargs);
  365.  
  366.    if(!err)
  367.       err = IoErr();
  368.  
  369.    if(err)
  370.    {
  371.       PrintFault(err,obj);
  372.       return(RETURN_ERROR);
  373.    }
  374.  
  375.    return(RETURN_OK);
  376. }
  377.  
  378.